package com.example.cdpbookstore.web.servlet;


import cn.hutool.core.util.IdUtil;
import com.example.cdpbookstore.pojo.entity.CartItem;
import com.example.cdpbookstore.pojo.entity.Customer;
import com.example.cdpbookstore.pojo.entity.Order;
import com.example.cdpbookstore.pojo.entity.OrderDetail;
import com.example.cdpbookstore.service.OrderService;
import com.example.cdpbookstore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/cart-check-out")
public class CheckOutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Customer customer = (Customer) request.getSession().getAttribute("customer");
        String orderId = IdUtil.randomUUID();
        LocalDateTime now = LocalDateTime.now();

        if (customer != null) {
            ArrayList<CartItem> cart = (ArrayList<CartItem>) request.getSession().getAttribute("cart");
            if (cart != null) {
                Order order = new Order();
                order.setId(orderId);
                order.setCustomerId(customer.getId());
                order.setCreationTime(now);
                order.setUpdateTime(now);

                List<OrderDetail> orderDetailList = new ArrayList<>();
                for (CartItem cartItem : cart) {
                    OrderDetail orderDetail = new OrderDetail();
                    orderDetail.setId(IdUtil.randomUUID());
                    orderDetail.setOrderId(orderId);
                    orderDetail.setBookId(cartItem.getBook().getId());
                    orderDetail.setNum(cartItem.getQuantity());
                    orderDetail.setUnitPrice(cartItem.getBook().getCurrentPrice());
                    orderDetail.setCreationTime(now);
                    orderDetail.setUpdateTime(now);

                    orderDetailList.add(orderDetail);
                }
                order.setOrderDetailList(orderDetailList);

                OrderService orderService = new OrderServiceImpl();
                boolean result = orderService.saveOrderWithDetail(order);
                if (result) {
                    cart.clear();
                    response.sendRedirect("order.jsp");
                } else {
                    out.println("订单生成失败！");
                }
            }
        } else {
            response.sendRedirect("login.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
